package zoo.productqnareplyboard.admin.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import zoo.member.admin.util.PagingUtil;
import zoo.productqnareplyboard.admin.dao.AdminQnAProductReplyBoardDao;
import zoo.productqnareplyboard.admin.domain.AdminQboardCommand;

@Controller
public class AdminProductBoardListController {
	private static Log log = LogFactory.getLog(AdminProductBoardListController.class);
	private int pageSize = 10;
	private int blockCount = 10;
	private String pageUrl = "qnalist.do";
	
	@Autowired
	private AdminQnAProductReplyBoardDao aqnaDao;
	
	@RequestMapping("/admin/qnalist.do")
	public ModelAndView process(@RequestParam(value="currentPage",defaultValue="1") int currentPage,
								@RequestParam(value="keyField",defaultValue="1")String keyField,
								@RequestParam(value="keyWord",defaultValue="1")String keyWord
								){
				
				String pagingHtml; //페이징을 구현한 HTML
				if(log.isDebugEnabled()){
				log.debug("currentPage : "+currentPage);
				}
				
				HashMap<String,Object> map = new HashMap<String,Object>();
				map.put("keyField", keyField);
				map.put("keyWord", keyWord);
				
				//총 글의 갯수 검색된 글의 갯수
				int count = aqnaDao.getQnaRowCount(map);
				
				PagingUtil page =new PagingUtil(pageUrl,keyField,keyWord,currentPage,count,blockCount,pageSize);
				
				pagingHtml = page.getPagingHtml().toString();
				
				map.put("start", page.getStartCount());
				map.put("end", page.getEndCount());
				
				List<AdminQboardCommand> list = null;
				if(count>0){
				list = aqnaDao.qnalist(map);
				}else{
				list = Collections.emptyList(); 
				}
				//글 목록에 표시할 연번
				int number = count-(currentPage-1)*pageSize; 
				
				ModelAndView mav = new ModelAndView();
				mav.setViewName("admin_qna_list");
				mav.addObject("count",count);
				mav.addObject("list",list);
				mav.addObject("pagingHtml",pagingHtml);
				mav.addObject("number",number);
				
				return mav;
	}
}
